from itsdangerous import TimedJSONWebSignatureSerializer as Serializer,BadSignature,SignatureExpired
from mall import settings


def generic_openid_token(opendid):

    # 1.创建序列化器
    s = Serializer(settings.SECRET_KEY,3600)

    # 2. 对 openid 进行处理
    token = s.dumps({'openid':opendid})

    # 因为 dumps 返回的是二进制
    openid_token = token.decode()

    return openid_token


def check_openid_token(access_token):
    s = Serializer(settings.SECRET_KEY,3600)
    try:
        dict = s.loads(access_token)

    except BadSignature:
        return None

    else:
        openid = dict.get('openid')

        return openid